package sort;

import java.util.Arrays;

/**
 * 排序
 *
 * @author cooper
 * @date 2019-08-14
 */
public class Sort {

    /**
     * 交换两个数
     */
    private static void swap(int[] arr, int i, int j) {
        if (i != j) {
            arr[i] = arr[i] ^ arr[j];
            arr[j] = arr[i] ^ arr[j];
            arr[i] = arr[i] ^ arr[j];
        }
    }

    /**
     * 冒泡排序
     */
    public static void bubbleSort(int[] array) {
        for (int end = array.length - 1; end > 0; end--) {
            for (int i = 0; i < end; i++) {
                if (array[i] > array[i + 1]) {
                    swap(array, i, i + 1);
                }
            }
        }
    }

    /**
     * 鸡尾酒冒泡排序 排序时，将最大的排到最后，同时将最小的排到最前边
     *
     * @param array
     */
    public static void bubbleSort2(int[] array) {
        int L = 0;
        int R = array.length - 1;
        while (L <= R) {
            //将最大的排到末尾
            for (int i = L; i < R; i++) {
                if (array[i] > array[i + 1]) {
                    swap(array, i, i + 1);
                }
            }
            R--;
            // 将最小的 排到最前边
            for (int i = R; i > L; i--) {
                if (array[i] < array[i - 1]) {
                    swap(array, i, i - 1);
                }
            }
            L++;
        }
    }

    /**
     * 选择排序
     *
     * @param array
     */
    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i; j < array.length; j++) {
                if (array[minIndex] > array[j]) {
                    minIndex = j;
                }
            }
            if (i != minIndex) {
                swap(array, i, minIndex);
            }
        }
    }

    /**
     * 插入排序
     */
    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int key = array[i], j;
            for (j = i - 1; j >= 0 && key < array[j]; j--) {
                array[j + 1] = array[j];
            }
            array[j + 1] = key;
        }
    }

    /**
     * 二分插入排序
     *
     * @param array
     */
    public static void insertSort2(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int key = array[i], L = 0, R = i - 1;
            // 二分找需要插入的位置
            while (L <= R) {
                int mid = L + (R - L) / 2;
                if (array[mid] < key) {
                    L = mid + 1;
                } else {
                    R = mid - 1;
                }
            }
            for (int j = i - 1; j >= L; j--) {
                array[j + 1] = array[j];
            }
            array[L] = key;
        }

    }

    /**
     * 快速排序
     */
    public static void quickSort(int[] array) {
        if (array == null || array.length <= 1) {
            return;
        }
        quickSort2(array, 0, array.length - 1);
    }

    private static void quickSort(int[] array, int left, int right) {
        if (left >= right) return;
        swap(array, left, left + (int) (Math.random() * (right - left + 1))); //随机选取一个pivot
        int mid = partition(array, left, right);
        quickSort(array, left, mid - 1);
        quickSort(array, mid + 1, right);
    }

    private static int partition(int[] array, int left, int right) {
        int key = array[left];
        int tempIndex = left;
        while (left < right) {
            // 从右边找到第一个小于key的index
            while (left < right && array[right] >= key) {
                right--;
            }
            //从左边找到第一个大于key的index
            while (left < right && array[left] <= key) {
                left++;
            }
            if (left >= right) break;
            swap(array, left, right);
        }
        swap(array, left, tempIndex);
        return left;
    }

    /**
     * 三路快排
     */
    private static void quickSort2(int[] array, int left, int right) {
        if (left >= right) return;
        swap(array, left, left + (int) (Math.random() * (right - left + 1))); //随机选取一个pivot
        int[] period = partition2(array, left, right);
        quickSort2(array, left, period[0] - 1);
        quickSort2(array, period[1] + 1, right);
    }

    private static int[] partition2(int[] array, int left, int right) {
        //用arr[L]作为划分点
        int key = array[left];
        int less = left, more = right + 1;
        int cur = left + 1;
        while (cur < more) {
            if (array[cur] < key) {
                swap(array, ++less, cur++);
            } else if (array[cur] > key) {
                swap(array, --more, cur++);
            } else {
                cur++;
            }
        }
        swap(array, left, less);
        return new int[]{less, more - 1};
    }


    /**
     * 归并排序
     */
    public static void mergeSort(int[] array) {
        if (array == null || array.length < 2) {
            return;
        }
        mergeSort(array, 0, array.length - 1);
    }

    private static void mergeSort(int[] array, int left, int right) {

        if (left >= right) return;
        int middle = left + (right - left) / 2;
        mergeSort(array, left, middle);
        mergeSort(array, middle + 1, right);
        if (array[middle] > array[middle + 1]) {
            merge(array, left, middle, right);
        }
    }

    private static void merge(int[] array, int left, int middle, int right) {

        int[] help = new int[right - left + 1];
        int k = 0;
        int p1 = left, p2 = middle + 1;
        //
        while (p1 <= middle && p2 <= right) {
            help[k++] = array[p1] <= array[p2] ? array[p1++] : array[p2++];
        }
        // [left, mid]剩下的放入help
        while (p1 <= middle) {
            help[k++] = array[p1++];
        }
        while (p2 <= right) {
            help[k++] = array[p2++];
        }
        // help[0,k] 放入array
        for (int i = 0; i < k; i++) {
            array[i + left] = help[i];
        }
    }

    /**
     * 堆排序
     */
    public static void heapSort(int[] array) {

        if (array == null || array.length < 2) {
            return;
        }
        int size = array.length - 1;
        for (int i = 0; i < array.length; i++) {
            siftUp(array, i);
        }
        swap(array, 0, size);
        while (size > 0) {
            siftDown(array, 0, size);
            swap(array, 0, --size);
        }

    }

    //上浮的过程  -->  把新插入的数调整为大根堆的过程
    private static void siftUp(int[] array, int index) {
        while (array[index] > array[(index - 1) / 2]) {
            swap(array, index, (index - 1) / 2);
            index = (index - 1) / 2;
        }
    }

    //下沉的过程  -->  这个函数就是一个数变小了，往下沉的函数,改变的数为index  目前的自己指定的堆的大小为heapSize
    static void siftDown(int[] arr, int i, int heapSize) {
        int L = 2 * i + 1;
        while (L < heapSize) {
            int maxIndex = L + 1 < heapSize && arr[L + 1] > arr[L] ? L + 1 : L;
            maxIndex = arr[i] > arr[maxIndex] ? i : maxIndex;
            if (maxIndex == i) break; //自己就是最大的，不用下沉
            //否则就要一直往下沉
            swap(arr, i, maxIndex);
            i = maxIndex;
            L = 2 * i + 1; //继续往下
        }
    }


    /**
     * @param args
     */
    public static void main(String[] args) {
        int[] arr = {10, 3, 21, 4, 36, 12, 19, 24, 20};
        quickSort(arr);
        System.out.println(Arrays.toString(arr));
    }
}
